package org.testfire.realm.control.agent.ResponseHandlers;

import org.apache.log4j.Logger;
import org.testfire.realm.control.testfire.TestFireDBAO;
import org.testfire.realm.model.entities.User;
import org.testfire.realm.model.entities.testfire.AgentEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 14, 2007
 * Time: 12:18:42 PM
 */
public class RegisterAgentResponseHandler
{
	private final TestFireDBAO testFireDBAO;

	public RegisterAgentResponseHandler(TestFireDBAO testFireDBAO)
	{
		this.testFireDBAO = testFireDBAO;
	}


	/**
	 * Agent is expected to be null for new agents. If this is an existing agent then it should have been discovered
	 * by its ID earlier in the processing.
	 *
	 * @param agent    An AgentEntity, possibly null if this is the first time the Agent has connected.
	 * @param user     The user that the AgentEntity is owned/managed by,
	 * @param request  The request from the Agent.
	 * @param response What will be sent back to the Agent.
	 * @throws Exception -
	 */
	public void perform(AgentEntity agent, User user, final HttpServletRequest request,
	                    final HttpServletResponse response) throws Exception
	{
		Logger log = Logger.getLogger(this.getClass());
		String agentName = request.getParameter("name");
		String description = request.getParameter("description");
		Boolean allowNonUserTests = Boolean.valueOf(request.getParameter("allowNonUserTests"));
		String agentAddress = request.getParameter("host");


		if(agent == null){
			log.info("New Agent '" + agentName + "'is attempting to register from host '" + agentAddress + "'.");
			agent = new AgentEntity();
			agent.setName(agentName);
			agent.setHost(agentAddress);
			agent.setDescription(description);
			agent.setAllownonusertests(allowNonUserTests);
			agent.setUser(user.getId());
			agent.setLastping(1);
			agent.setLastPingTime(System.currentTimeMillis());
			testFireDBAO.persist(agent);
			agent = testFireDBAO.getAgentByHost(agent.getHost());
			response.addHeader("agent_id", agent.getId().toString());			
			response.addHeader("response", "Successfully registered new Agent. Welcome Agent #" + agent.getId());
		}
		else{
			log.info("Existing agent "  + agent.getId() + " is attempting to register " +
					"from host '" + agentAddress + "'." );
			agent.setLastPingTime(System.currentTimeMillis());
			if (!agent.getName().equals(agentName))
			{
				agent.setName(agentName);
			}
			if (!agent.getHost().equals(agentAddress))
			{
				agent.setHost(agentAddress);
			}
			if (!agent.getDescription().equals(description))
			{
				agent.setDescription(description);
			}
			if (!agent.getAllownonusertests().equals(allowNonUserTests))
			{
				agent.setAllownonusertests(allowNonUserTests);
			}
			testFireDBAO.merge(agent);
			response.addHeader("message", "Successfully updated Agent #" + agent.getId());
			response.addHeader("agent_id", agent.getId().toString());
			response.addHeader("response", "Welcome back agent #" + agent.getId());			
		}




	}
}